package br.com.marcelo.reservasis.server.service;

import br.com.aroeirasoftware.gwtcomponents.client.query.ASQuery;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQueryResult;
import br.com.aroeirasoftware.gwtcomponents.domain.facades.ASGWTFacade;
import br.com.aroeirasoftware.gwtcomponents.domain.facades.FacadeException;
import br.com.aroeirasoftware.gwtcomponents.server.services.ASBaseUIServiceImpl;
import br.com.marcelo.reservasis.client.services.apartamento.ApartamentoUIService;
import br.com.marcelo.reservasis.domain.facade.ApartamentoFacade;
import br.com.marcelo.reservasis.domain.model.Apartamento;
import br.com.marcelo.reservasis.domain.model.TipoApartamento;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Marcelo Rodrigues
 */
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class ApartamentoUIServiceImpl extends ASBaseUIServiceImpl implements ApartamentoUIService {

    private static final long serialVersionUID = 1L;
    @Resource(name = "baseGWTFacade")
    private ASGWTFacade<Apartamento, Integer> facade;
    @Resource(name = "apartamentoFacade")
    private ApartamentoFacade<TipoApartamento, Integer> apartamentoFacade;

    public Integer salvar(Apartamento arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_SALVAR, arg0);

        try {
            return facade.salvar(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_SALVAR, e);
            throw new RuntimeException(e.getMessage());
        }
    }

    public void atualizar(Apartamento arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_ATUALIZAR, arg0);

        try {
            facade.atualizar(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_ATUALIZAR, e);
            throw new RuntimeException(e.getMessage());
        }
    }

    public void excluir(Apartamento arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_EXCLUIR, arg0);

        try {
            facade.excluir(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_EXCLUIR, e);
            throw new RuntimeException(e.getMessage());
        }
    }

    public Apartamento getPorId(Integer arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_OBTER_POR_ID, arg0);

        try {
            facade.setPersistentClass(Apartamento.class);
            return facade.getPorId(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_OBTER_POR_ID, e);
            throw new RuntimeException(e.getMessage());
        }
    }

    public ASQueryResult<Apartamento> getPorConsulta(ASQuery arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_CONSULTAR, arg0);

        try {
            return facade.getPorConsulta(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_CONSULTAR, e);
            throw new RuntimeException(e.getMessage());
        }
    }

    public ASQueryResult<TipoApartamento> getTipoApartamentoPorConsulta(ASQuery arg0) throws RuntimeException {
        logInfo(MSG_INVOCANDO_FACADE_PARA_CONSULTAR, arg0);

        try {
            return apartamentoFacade.getPorConsulta(arg0);
        } catch (FacadeException e) {
            logErro(MSG_ERRO_CONSULTAR, e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
